﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using Webapiv3.Domain;
using Webapiv3.Domain.Entity;
using Webapiv3.Implement;
using Webapiv3.inetrface;

namespace Webapiv3.Utils
{
    public class Dbinitialize
    {
        public static void Seed(IServiceProvider serviceProvider)
        {
            using(var scope = serviceProvider.CreateScope())
            {
                    var db = scope.ServiceProvider.GetService(typeof(Admin3000Context))as Admin3000Context;
                    var _Respository = scope.ServiceProvider.GetService(typeof(IRespository<User>)) as Efrespository<User>;
                    var _roleRespository = scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as Efrespository<Roles>;
                    db.Database.EnsureCreated();
                    var hasUsers = db.Users.Any();
                    if (!hasUsers)
                    {
                        var role = new Roles
                        {
                            RolesName = "管理员",
                            Description = "是一个管理员"
                        };
                        _roleRespository.Insert(role);
                        //db.Roles.Add(role);
                        //db.SaveChanges();
                        _Respository.InsertBulk(new User[]
                        {
                        new User
                        {
                            UserName="admain",
                            PassWord="123",
                            RolseId=role.Id,
                        },
                        new User
                        {
                            UserName="admain02",
                            PassWord="12356",
                            RolseId=role.Id,
                        }

                        });

                }
            }
        }
    }
}
